package ngat.oss;

import ngat.message.OSS.*;
import ngat.instrument.*;
import ngat.phase2.*;

import java.io.*;

/** 
 * Action: Updates the Seeing prediction model used for scheduling.
 * <br><br>
 * $id$
 */
public class UPDATE_SEEINGImpl extends TransactionImpl implements Serializable {

 /** Stores the set of values to use for updating the seeing model.*/
    protected double value;

    /** Stores the set of times at which the values array elements were taken.*/
    protected long time;

    /** Update type.*/
    protected int type;

    /** Create an UPDATE_SEEINGImpl for an unspecified request.*/
    public UPDATE_SEEINGImpl ( long time, double value) {
	super();
	this.time  = time;
	this.value = value;
	this.type = type;
    }
    
    /** Create an UPDATE_SEEINGImpl for the specified request.
     * @param request The request to handle.*/
    public UPDATE_SEEINGImpl (UPDATE_SEEING request) {
	super(request);	
	this.time  = request.getTime();
	this.value = request.getValue();
	this.type   = request.getType();
    }
    
    /**
     * Update the seeing prediction model.   
     */
    public TRANSACTION_DONE exec() {
		
	UPDATE_SEEING_DONE done = new UPDATE_SEEING_DONE(id);

	switch (type) {

	case UPDATE_SEEING.UPDATE_SEEING:
	    
	    int seeing = Group.POOR;
	    String  ss = "POOR";

	    if (value < 0.8) {
 		seeing =Group.EXCELLENT;
		ss  = "EXCELLENT (seeing < 0.8 arcsec)";
 	    } else if 
		(value < 1.3) {
		seeing = Group.AVERAGE;	
		ss  = "AVERAGE (0.8 < seeing < 1.3)";	
	    } else if 
		(value < 3.0){
		seeing = Group.POOR;
		ss  = "POOR (1.3 < seeing < 3.0 arcsec)";
	    } else {
		seeing = Group.CRAP;
		ss = "CRAP (seeing > 3.0 arcsec)";
	    }

	    Scheduling.setSeeingPrediction(seeing);
	    Scheduling.setCorrectedSeeingValue(value);

	    System.err.println("UPDATE_SEEING: prediction is: "+ss+" using (R-Band at zenith): "+value);
	    	    
	    done.setPrediction(seeing);
	    done.setSuccessful(true);
	    return done;
	  
	case UPDATE_SEEING.UPDATE_PHOTOM:
	    
	    int photom = Group.POOR;
	    String  pp = "NON_PHOTOMETRIC";
	
	    if (value < 0.2) {
 		photom =Group.EXCELLENT;
		pp  = "PHOTOMETRIC(extinction < 20% cirrus)";
 	    } else {
		photom = Group.POOR;	
		pp  = "NON_PHOTOMETRIC (extinction > 20%  cirrus)";	
	    }

	    Scheduling.setPhotomPrediction(photom);
	 
	    System.err.println("UPDATE_PHOTOM: prediction is: "+pp);
	    	    
	    done.setPrediction(photom);
	    done.setSuccessful(true);
	    return done;
	  
	}

	done.setErrorNum(UPDATE_SEEING.UNKNOWN_TYPE);
	done.setErrorString("Unknown prediction type: "+type);
	done.setSuccessful(false);
	return done;
	
    }
    
}

/** $Log: UPDATE_SEEINGImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.2  2001/04/27 17:22:00  snf
/** Adda proposal.
/** */
